<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Account;

use AuctionCore\System\AuctionException;
use \AuctionCore\Common\PhpMemcache;

/**
 * 帐号功能抽像类
 * @author       Administrator
 */
abstract class User {

    /**
     * 用户名
     * @var      String
     */
    protected $user_name;
    protected $uid;

    /**
     * 授权管理功能类
     * @var      int
     */
    protected $auth_object;

    /**
     * 构造
     * @param    String $user_name    用户名
     * @return   void
     */
    public function __construct($user_name) {
//        if (is_numeric($user_name)) {
//            $m = $this->_getModel();
//            if (strlen($user_name) > 10) {
//                //手机登录
//                $uid_where = array('pwd_mobile' => $user_name);
//            } else {
//                //uid登录
//                $uid_where = array('uid' => $user_name);
//            }
//            $uid_where = array_merge($uid_where, array('status' => 0));
//            $info = $m->where($uid_where)->find();
//            if (!$info['user_name'])
//                AuctionException::make_throw('200008');
//            $user_name = $info['user_name'];
//        }
        //用户名登录
        $this->user_name = $user_name;
    }

    private function _getModel() {
        $auth_db_cfg = $this->auth_object->get_config();

        $m = M($auth_db_cfg["AUTH_USER"]);
        return $m;
    }

    /**
     * 配合属性“用户名”，修改密码 
     * 返回:true/false
     * 
     *   
     * @param    String $new_pwd    新密码
     * @return   boolean
     */
    public function change_password($new_pwd) {

        if (!$this->user_name)
            AuctionException::make_throw('200003');
        if (empty($new_pwd))
            AuctionException::make_throw('200004');

        $m = $this->_getModel();
        $sets = array('passwd' => md5($new_pwd));
        $ok = $m->where(array('user_name' => $this->user_name))->save($sets);
        return $ok;
    }

    /**
     * 配合属性“用户名”，修改密码 
     * 返回:true/false
     * 
     *   
     * @param    String $new_pwd    新密码
     * @return   boolean
     */
    public function change_md5_password($new_pwd) {

        if (!$this->user_name)
            AuctionException::make_throw('200003');
        if (empty($new_pwd))
            AuctionException::make_throw('200004');

        $u_info = $this->user_info();
        if($u_info['passwd']==$new_pwd)
            AuctionException::make_throw('200038');            

        $m = $this->_getModel();
        $sets = array('passwd' => $new_pwd);
        $ok = $m->where(array('user_name' => $this->user_name))->save($sets);
        return $ok;
    }

    /**
     * 检查密码是否正确，检查启用状态，登录或修改密码使用。
     * 返回：用户信息
     * @param    String $pwd    帐号密码
     * @return   Object
     */
    public function check_pwd($pwd) {
        $m = $this->_getModel();
        $info = $m->where("user_name='{$this->user_name}'")->find();
        if ($info['passwd'] == md5($pwd))
            return $info;
        else
            return false;
    }

    /**
     * 检查密码是否正确，检查启用状态，登录或修改密码使用。
     * 返回：用户信息
     * @param    String $pwd    帐号密码
     * @return   Object
     */
    public function check_md5_pwd($pwd) {
        $m = $this->_getModel();
        $info = $m->where("user_name='{$this->user_name}'")->find();
        if ($info['passwd'] == $pwd)
            return $info;
        else
            return false;
    }

    /**
     * 用户详细
     * @return   Object
     */
    public function user_info() {
        $m = $this->_getModel();
        $info = $m->where(array('user_name' => $this->user_name))->find();
        return $info;
    }

    /**
     * 用户详情
     * return Object
     */
    public function get_user_info_by_uid($uid){
        $m = $this->_getModel();
        $info = $m->where(array('uid' => $uid))->find();
        return $info;
    }
    /**
     * 用户详情
     * return Object
     */
    public function get_user_info_by_pwd_mobile($pwd_mobile){
        $m = $this->_getModel();
        $info = $m->where(array('pwd_mobile' => $pwd_mobile))->find();
        return $info;
    }
    /**
     * 修改帐号手机号码
     * @param    String $mobile    新手机号
     * @return   boolean
     */
    public function change_pwd_mobile($mobile) {
        return $this->change_mobile($mobile, 'pwd_mobile');
    }

    public function check_id_card($id_card){
        $m = $this->_getModel();
        return $m->where(['u_idcard'=>$id_card])->find();
    }

    /**
     * 修改短信手机号码
     * @param    String $mobile    新手机号
     * @return   boolean
     */
    public function change_sms_mobile($mobile) {
        return $this->change_mobile($mobile, 'sms_mobile');
    }

    private function change_mobile($mobile, $type = 'sms_mobile') {
        if (empty($mobile))
            AuctionException::make_throw('200007');
        try{
            $m = $this->_getModel();
            $sets = array($type => $mobile);
            $ok = $m->where(array('user_name' => $this->user_name))->save($sets);    
        }catch(\Exception $e){                        
            if($e->getCode()=='23000'){
                AuctionException::make_throw('200076');
            }else{
                throw $e;
            }
        }
        return $ok;
    }

    public function update($sets) {
        $m = $this->_getModel();
        $user_name = $this->user_name;
        $ok = $m->where("user_name='{$user_name}'")->save($sets);
        return $ok;
    }

    /**
     * 获取用户是否禁用状态
     * @param $uid
     */
    public function check_user_status($uid){
        $m = $this->_getModel();
        $status = $m->where(array('uid'=>$uid))->getField('status');
        if($status == 1){
            return false;
        }
        return true;
    }

    /**
     * 启用帐号
     * @return   void
     */
    public function enable() {
        $m = $this->_getModel();
        $ok = $m->where(array('user_name' => $this->user_name))->save(array('status' => '0'));
    }

    /**
     * 禁用帐号
     * @return   void
     */
    public function disable() {
        $m = $this->_getModel();
        $ok = $m->where(array('user_name' => $this->user_name))->save(array('status' => '1'));
    }

    /**
     * 设置员工所属分组
     * @param    int $group_id    组id
     * @return   int
     */
    public function set_group($group_id) {
        $user_info = $this->user_info();
        $group_users = array('uid' => $user_info['uid'], 'group_id' => $group_id);

        $auth_db_cfg = $this->auth_object->get_config();
        $m = M($auth_db_cfg["AUTH_GROUP_ACCESS"]);
        $ok = $m->add($group_users);
    }
    //用组标识设置用户属组
    public function set_group_by_title($title){
        $auth_db_cfg = $this->auth_object->get_config();
        $m = M($auth_db_cfg["AUTH_GROUP"]);
        $group_id = $m->getFieldByTitle($title,'id');
        return $this->set_group($group_id);
    }

    /**
     * 员工移出所属分组
     * @param    int $group_id    组id
     * @return   int
     */
    public function exgraft_group($group_id) {
        $user_info = $this->user_info();
        $auth_db_cfg = $this->auth_object->get_config();
        $m = M($auth_db_cfg["AUTH_GROUP_ACCESS"]);
        $where = "uid='{$user_info['uid']}' and group_id='{$group_id}'";
        $ok = $m->where($where)->delete();
        return $ok;
    }


}